package com.finace.management.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.finace.mapper.TbModelMapper;
import com.finace.pojo.TbModel;
import com.finace.pojo.TbModelExample;
import com.finace.pojo.TbModelExample.Criteria;
import com.finace.management.service.ModelService;

import com.entity.PageResult;

/**
 * 服务实现层
 * @author Administrator
 *
 */
@Service
public class ModelServiceImpl implements ModelService {

	@Autowired
	private TbModelMapper modelMapper;
	
	/**
	 * 查询全部
	 */
	@Override
	public List<TbModel> findAll() {
		return modelMapper.selectByExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<TbModel> page=   (Page<TbModel>) modelMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 增加
	 */
	@Override
	public void add(TbModel model) {
		modelMapper.insert(model);		
	}

	
	/**
	 * 修改
	 */
	@Override
	public void update(TbModel model){
		modelMapper.updateByPrimaryKey(model);
	}	
	
	/**
	 * 根据ID获取实体
	 * @param id
	 * @return
	 */
	@Override
	public TbModel findOne(int id){
		return modelMapper.selectByPrimaryKey(id);
	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(int[] ids) {
		for(int id:ids){
			modelMapper.deleteByPrimaryKey(id);
		}		
	}
	
	
		@Override
	public PageResult findPage(TbModel model, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		
		TbModelExample example=new TbModelExample();
		Criteria criteria = example.createCriteria();
		
		if(model!=null){			
						if(model.getModelName()!=null && model.getModelName().length()>0){
				criteria.andModelNameLike("%"+model.getModelName()+"%");
			}
	
		}
		
		Page<TbModel> page= (Page<TbModel>)modelMapper.selectByExample(example);		
		return new PageResult(page.getTotal(), page.getResult());
	}
	
}
